package com.luxsoft.siipap.migracion.parches;

import java.sql.Types;
import java.util.List;
import java.util.Map;

import com.luxsoft.siipap.service.ServiceLocator2;

public class ReporteEspecialDeCostosDeVenta {
	
	
	public static void actualizarUltimoCosto(){
		String sql="select CLAVE from REPORTE_ESPECIAL_COSTOV where NACIONAL=1";
		List<String> claves=ServiceLocator2.getAnalisisJdbcTemplate().queryForList(sql,String.class);
		for(String s:claves){
			acutalizarUltimoCosto(s);
		}
	}
	
	public static void acutalizarUltimoCosto(String clave){
		String sql="select b.FECHA as FECHA,a.ARTCLAVE,a.neto*b.tc as NETO from SW_ANALISISDET a join SW_ANALISIS b on(a.ANALISIS_ID=b.ANALISIS_ID) where a.ARTCLAVE=? order by b.fecha desc";
		List<Map<String, Object>> data=ServiceLocator2.getAnalisisJdbcTemplate().queryForList(sql,new Object[]{clave});
		if(!data.isEmpty()){
			Map<String, Object> row=data.get(0);
			Number costo=(Number)row.get("NETO");
			row=data.get(0);
			String updateSql="update REPORTE_ESPECIAL_COSTOV set " +
			"COSTO=? " +
			",FECHAU=?" +
			"where CLAVE=? ";
			int res=ServiceLocator2.getAnalisisJdbcTemplate().update(
					updateSql
					,new Object[]{costo.doubleValue(),row.get("FECHA"),clave
							}
					,new int[]{Types.NUMERIC,Types.DATE,Types.VARCHAR}
					);
			System.out.println("Clave: "+clave+" U.Costo: "+row.get("NETO")+" Update: "+res);
		}
		
	}
	
	public static void main(String[] args) {
		actualizarUltimoCosto();
	}

}
